/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUIComponents.monthlyComponents;

import MainClasses.*;
import MainClasses.Event.Event;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.*;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;

/**
 *
 * @author andrewcutler
 */
public class MonthlyViewPanel extends JPanel {
    
    private JPanel panel;
    private CourseManager manager;
    
    
    public MonthlyViewPanel(CourseManager manager)
    {
        super();
        this.setName("Monthly View");
        this.manager = manager;
        //panel = (JPanel) super.getViewport().getView();
        init();
        
    }
    private void init()
    {
        //this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        
        
        JPanel tempPanel = new JPanel();
        tempPanel.setLayout(new GridLayout(1,7));
        
        String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        
        for(int i = 0; i<days.length; i++)
        {
            tempPanel.add(new HeaderPanel(days[i]));
        }
        c.fill = GridBagConstraints.HORIZONTAL;
	c.gridx = 0;
	c.gridy = 0;
        //this.add(tempPanel, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
	c.ipady = 0;       //reset to default
	c.weighty = 1.0;   //request any extra vertical space
	c.gridx = 0;       //aligned with button 2
	c.gridwidth = 1;   //2 columns wide
	c.gridy = 1;       //third row
        this.add(new MainPanel());
        
    }

    public void reset()
    {
        this.removeAll();
        //panel = new JPanel();
        //super.getViewport().setView(panel);
        init();
    }
    private class HeaderPanel extends JPanel{
        public HeaderPanel(String day)
        {
            super();
            this.add(new JLabel(day));
            this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
    }
            
    
    private class FillerPanel extends JPanel {
        
        public FillerPanel()
        {
            super();
            //this.setBorder(BorderFactory.createLineBorder(Color.MAGENTA));
        }
    }
    private class MonthPanel extends JScrollPane {
        
        public MonthPanel(MyDate date, CourseManager m)
        {
            super(new JPanel());
            this.setBorder(BorderFactory.createLineBorder(Color.black));
            
            this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            JPanel myPanel = (JPanel) super.getViewport().getView();
            
            myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.PAGE_AXIS));
            myPanel.add(new JLabel(date.getStringedMonth() + " - " + String.valueOf(date.getDayOfMonth())));
            
                for(Event a : m.getAllEvents())
                {
                    MyDate dateDue = a.getEndDate();
                    if(dateDue.equals(date))
                    {
                        myPanel.add(a.getPanel());
                    }
                }
            
        }
    }
    
    private class MainPanel extends JPanel {
        
        public MainPanel()
        {
            super();
            init();
        }
        private void init()
        {
                    

            MyDate firstOfMonth = new MyDate(1, main.getCurrentDate().getMonth(), main.getCurrentDate().getYear());
            
            this.setLayout(new GridLayout(NumberRowsNeeded(firstOfMonth), 7));
            
            int dayNum = 0;
            int dayMonthStarts = firstOfMonth.getDay();
            int daysInMonth = firstOfMonth.getDaysInMonth();
            System.out.println(dayMonthStarts + " - " + daysInMonth);

            

            for(int i = 0; i < dayMonthStarts; i++)
            {
                System.out.println(dayNum);
                this.add(new FillerPanel());
                dayNum++;
            }
            for(int j = 0; j < daysInMonth; j++)
            {
                this.add(new MonthPanel(new MyDate(j+1, main.getCurrentDate().getMonth(), main.getCurrentDate().getYear()), manager));
                dayNum++;
            }
            //for(int k = dayNum; k < 6*7; k++)
            //{
            //    this.add(new FillerPanel());
            }
        }
        
        
        /*
     USE:  Calculates number of rows needed for calendar.
     IN:   year = given year after 1582 (start of the Gregorian calendar).
           month = 0 for January, 1 for February, etc.
     OUT:  Number of rows: 5 or 6, except for a 28 day February with
           the first of the month on Sunday, requiring only four rows.
  */
        int NumberRowsNeeded(MyDate date)
        {
            int firstDay;       /* day of week for first day of month */
            int numCells;       /* number of cells needed by the month */

            int year = date.getYear();
            int month = date.getMonth()-1;
            
            /* Start at 1582, when modern calendar starts. */
            if (year < 1582) return (-1);

            /* Catch month out of range. */
            if ((month < 0) || (month > 11)) return (-1);

            /* Get first day of month. */
            firstDay = date.CalcFirstOfMonth(year, month);

            /* Non leap year February with 1st on Sunday: 4 rows. */
            if ((month == 1) && (firstDay == 0) && !MyDate.IsLeapYear(year))
              return (4);

            /* Number of cells needed = blanks on 1st row + days in month. */
            numCells = firstDay + MyDate.DaysInMonth[month];

            /* One more cell needed for the Feb 29th in leap year. */
            if ((month == 1) && (MyDate.IsLeapYear(year))) numCells++;

            /* 35 cells or less is 5 rows; more is 6. */
            return ((numCells <= 35) ? 5 : 6);
        } // NumberRowsNeeded
  
    
    
    
    
    
    
}
